package com.leyou.item.controller;

import com.leyou.common.vo.PageResult;
import com.leyou.item.dto.BrandDTO;
import com.leyou.item.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("brand")
public class BrandController {
    @Autowired
    private BrandService brandService;


    @GetMapping("page")
    public ResponseEntity<PageResult<BrandDTO>> queryBrandByPage(
            @RequestParam(value = "page", defaultValue = "1")Integer page,
            @RequestParam(value = "rows", defaultValue = "5")Integer rows,
            @RequestParam(value = "key", required = false)String key,
            @RequestParam(value = "sortBy", required = false)String sortBy,
            @RequestParam(value = "desc", defaultValue = "false")Boolean desc
    ){
        return ResponseEntity
                .ok(brandService.queryBrandByPage(page,rows, key, sortBy, desc));
    }

    /*
     * @User Administrator
     * @Description //TODO  保存操作
     * @Date 15:04 2019/8/11 0011
     * @Param
     * @return
     **/
    @PostMapping
    public ResponseEntity<Void> saveBrand(BrandDTO brandDTO, @RequestParam("cids") List<Long> ids) {
        brandService.saveBrand(brandDTO, ids);
        return ResponseEntity.status(HttpStatus.CREATED).build();//返回一个状态码,给前端一个友好提示 表示保存成功

    }


    /**
     * 修改品牌
     * @param brandDTO
     * @return
     */
    @PutMapping
    public ResponseEntity<Void> updateBrand(BrandDTO brandDTO, @RequestParam("cids") List<Long> ids) {
        brandService.updateBrand(brandDTO, ids);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    @GetMapping("deleteBrand")
    public ResponseEntity<Object> saveBrand(@RequestParam("id") Long id) {
        brandService.deleteBrand(id);
        return ResponseEntity.status(HttpStatus.CREATED).build();//返回一个状态码,给前端一个友好提示 表示保存成功

    }

    @GetMapping("/of/category")
    public ResponseEntity<List<BrandDTO>> queryByCategoryId(@RequestParam("id") Long categoryId) {
        return ResponseEntity.ok(this.brandService.queryByCategoryId(categoryId));
    }

    /**
     * 根据id的集合查询品牌
     * @param ids 品牌id的集合
     * @return 品牌的集合
     */
    @GetMapping("/list")
    public ResponseEntity<List<BrandDTO>> queryBrandByIds(@RequestParam("ids") List<Long> ids){
        return ResponseEntity.ok(brandService.queryByIds(ids));
    }

    /**
     * 根据id的集合查询品牌
     * @param id 品牌id
     * @return 品牌的集合
     */
    @GetMapping("/id")
    public ResponseEntity<BrandDTO> queryBrandById(@RequestParam("id") Long id){
        return ResponseEntity.ok(brandService.queryById(id));
    }
}
